import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

public class ProcessManager {

	public ProcessManager() {
	}

	public int ExecProcess(String processToExec, boolean withPath, String path) {
		try {
			Runtime runtime = Runtime.getRuntime();
			Process process;
			
			if (withPath)
			{
				String cdCommand = "cmd /C CD " + "\"" + path + "\"";
				process = runtime.exec(cdCommand);
				process = runtime.exec(processToExec, null, new File(path));
			}
			else
			{
				process = runtime.exec(processToExec);
			}
			InputStream is = process.getInputStream();
			InputStreamReader isr = new InputStreamReader(is);
			BufferedReader br = new BufferedReader(isr);
			String line;

			System.out.println("\nProcess " + processToExec + " start ");
			while ((line = br.readLine()) != null) {
				System.out.println(line);
			}
			int exitVal = process.waitFor();
			System.out.println("\nProcess exitValue: " + exitVal);
			return exitVal;
		} catch (IOException ex1) {
			ex1.printStackTrace();
			System.err.println(ex1.getMessage());
			return -9001;
		} catch (InterruptedException ex2) {
			ex2.printStackTrace();
			System.err.println(ex2.getMessage());
			return -9002;
		} catch (Exception ex3) {
			ex3.printStackTrace();
			System.err.println(ex3.getMessage());
			return -9003;
		}
	}
}
